/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.service;

import com.bookshop.dao.IDaoAuthorLocal;
import com.bookshop.dao.IDaoBookLocal;
import com.bookshop.dao.IDaoCategoryLocal;
import com.bookshop.dao.IDaoCollectionLocal;
import com.bookshop.dao.IDaoPublisherLocal;
import com.bookshop.dao.IDaoSerieLocal;
import com.bookshop.dao.IDaoTypeLocal;
import com.bookshop.entity.Author;
import com.bookshop.entity.Book;
import com.bookshop.entity.Category;
import com.bookshop.entity.Collection;
import com.bookshop.entity.Publisher;
import com.bookshop.entity.Serie;
import com.bookshop.entity.Type;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

/**
 *
 * @author Thibault
 */
@Singleton(mappedName = "com.bookshop.service")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class BookService implements Serializable {

    @EJB
    private IDaoBookLocal daoBook;
        
    @EJB
    private IDaoCollectionLocal daoCollection;
        
    @EJB
    private IDaoPublisherLocal daoPublisher;
        
    @EJB
    private IDaoSerieLocal daoSerie;
        
    @EJB
    private IDaoAuthorLocal daoAuthor;  
    
    @EJB
    private IDaoTypeLocal daoType;  
    
    @EJB
    private IDaoCategoryLocal daoCategory;  
    
    public List<Book> getAllBooks() {
        return daoBook.getAllBooks();
    }

    public List<Book> getAllBooksByCategoryId(int categoryId) {
        return daoBook.getAllBooksByCategoryId(categoryId);
    }

    public Book getBookByISBN(String isbn) {
        return daoBook.getBookByISBN(isbn);
    }
    
    public List<Book> getBookByTitle(String title) {
        return daoBook.getAllBooksByTitle(title);
    }
    public List<Book> simpleSearch(String title){
        return getBookByTitle(title);
    }
    
        public List<Book> advancedSearch(String title, String publisher, String collection, String author, String serie, int typeId, int categoryId){

            List<Book> finalList = new ArrayList<Book>();
            List<Book> temporaryList = new ArrayList<Book>();
            
            if (title != null){
                if (!title.trim().equals("")){
                    finalList.addAll(getBookByTitle(title));
                }
            }
            
            if (author != null){
                if (!author.trim().equals("")){
                    //on récupère les auteurs pouvant correspondre
                    List<Author> authorList = daoAuthor.getAuthorByName(author.trim());

                    //on récupère les livres des dits auteurs
                    for (Author a : authorList){
                        temporaryList = daoBook.getAllBooksByAuthor(a);
                    }
                    //on retire de la liste finale ceux qui ne correspondent pas à TOUS les critères
                    finalList = jonctionList(finalList, temporaryList); 
                }     
            }

            if (publisher != null){
                if (!publisher.trim().equals("")){

                    for (Publisher p : daoPublisher.getPublisherByName(publisher.trim())){
                        temporaryList = daoBook.getAllBooksByPublisher(p);
                    }
                    finalList = jonctionList(finalList, temporaryList);  
                }
            }
            
            if (collection != null){
                
                if (!collection.trim().equals("")){
                    for (Collection c : daoCollection.getCollectionByName(collection.trim())){
                        temporaryList = daoBook.getAllBooksByCollection(c);
                    }
                    finalList = jonctionList(finalList, temporaryList);   
                }
            }
            
            if (serie != null){
                if (!serie.trim().equals("")){

                    for (Serie s : daoSerie.getSerieByName(serie)){
                        temporaryList = daoBook.getAllBooksBySerie(s);
                    }
                    finalList = jonctionList(finalList, temporaryList);
                }      
            }
            
            if (typeId != -1){
                Type t = daoType.getTypeById(typeId);
                temporaryList = daoBook.getAllBooksByType(t);
                finalList = jonctionList(finalList, temporaryList);      
            }
            
            if (categoryId !=-1){
                Category c = daoCategory.getCategoryById(categoryId);
                temporaryList = daoBook.getAllBooksByCategory(c);
                finalList = jonctionList(finalList, temporaryList);      
            }   
        return finalList;
    }

    private List<Book> jonctionList(List<Book> finalList, List<Book> temporaryList) {
        
        if (finalList.isEmpty()){
            finalList.addAll(temporaryList);
        }else if (!temporaryList.isEmpty()){
            
            List<Book> deleteList = new ArrayList<Book>();

            for (Book b : finalList){

                if (!temporaryList.contains(b)){
                    deleteList.add(b);
                }
            }
            finalList.removeAll(deleteList);
        }

        return finalList;
    }
    
}
